<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/4/23-14:11
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */
namespace App\Http\Controllers\Web;

use App\ConstDir\ErrorConst;
use App\Repository\Repositories\Interfaces\OrdersCrontabRepository;
use Illuminate\Http\Request;
use App\Events\OrderAffirmEvent;

class OrdersCrontabController extends BaseController
{
    protected $ordersCrontab_repo;

    public function __construct(OrdersCrontabRepository $ordersCrontab_repo)
    {
        $this->ordersCrontab_repo = $ordersCrontab_repo;
        $this->status = 0;
    }

    /**
     * @params Request
     * TODO 默认获取未进行确认的列表信息
     * @return JSON
     * */
    public function index(Request $request)
    {
        dd('index');
        $type = $request->input("type","all");
        if($type == "all"){
            // 根据需要确定是否需要分页
            $ordersCrontab = $this->ordersCrontab_repo->findWhere(['status'=>$this->status]);
            if(isset($ordersCrontab['data'])){
                $ret['list'] = $ordersCrontab['data'];
            }else{
                $ret['list'] = [];
            }
            return $this->success(ErrorConst::SUCCESS_CODE, $ret,false);
        }else{
            $attr_values = $this->ordersCrontab_repo->paginate($this->perPage);
            return $this->success(ErrorConst::SUCCESS_CODE, $attr_values,true);
        }
    }
    /**
     *
     * */
    public function show($id){}
    /**
     * TODO 创建新的需要确认订单的信息
     *
     * */
    public function store(Request $request)
    {
        // 新增确认信息
        $this->validate($request, [
            'expired_at' => 'required',
            'user_id'    => 'required|integer',
            'shop_id'    => 'required|integer',
            'order_id'   => 'required|integer',
            'order_code' => 'required|string',
            'order_money'=> 'required|numeric',
        ],[
            'expired_at' => '过期时间是必需字段',
            'user_id'    => '用户id 是必需字段|用户id类型必须是整型',
            'shop_id'    => '商铺id 是必需字段|商铺id类型必须是整型',
            'order_id'   => '订单id 是必需字段|订单id类型必须是整型',
            'order_code' => '订单编号是必填字段|订单编号必须是字符串类型',
            'order_money'=> '订单金额是必填字段|订单金额必须是数字',
        ]);
        $data = $request->all(['expired_at','user_id','shop_id','order_id','order_code','order_money']);
        $data['status'] = '0';
        $ret = $this->ordersCrontab_repo->create($data);

        return $this->noDataReturn($ret ? true : false);
    }

    /**
     * TODO 修改订单为确认状态
     *
     * */
    public function update($id)
    {
//        dd($id);
        \DB::beginTransaction();
        try{
            // 获取 @table orders_crontabs 信息
            $ordersCrontab = $this->ordersCrontab_repo->find($id);
            $ordersCrontab = $ordersCrontab['data'];
            if ($ordersCrontab['status']==1){
                throw new \Exception('该订单已确认完成。');
            }else{
                $order_id = isset($ordersCrontab['order_id'])?$ordersCrontab['order_id']:false;

                if ($order_id==false){
                    throw new \Exception('未正确获取到订单号');
                }
                // 修改订单状态 && 通知银行放款 && 修改本表状态为已确认
                $confirmEvent = event(new OrderAffirmEvent(['order_id' => $order_id]));
                if ($confirmEvent!==true){
                    throw new \Exception('订单状态修改失败');
                }
                // 修改本表状态为已确认
                $update = $this->ordersCrontab_repo->update(['status'=>1],$id);
            }
            $ret = isset($update)?$update:false;
        } catch (\Exception $e) {
            \DB::rollback(); //事务回滚;
            return $this->error($e->getMessage());
        }
        \DB::commit();
        // 返回执行信息
        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id){}
}
